package com.platform.modules.mall.activity.restrict;

import com.alibaba.fastjson.JSONArray;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Constant;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.activity.entity.bo.ActivityResultStatus;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.service.MallActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 验证活动信息
 */
@Component
public class ActivityVerify {

    @Autowired
    private MallActivityService mallActivityService;

    public MallActivityEntity getActivity(String id){
        return mallActivityService.getById(id);
    }

    public MallActivityEntity verifyContext(String activityId) {
        if (StringUtils.isBlank(activityId)){
            throw new BusinessException("活动id不能为空");
        }
        /*获得活动信息*/
        MallActivityEntity activityEntity = mallActivityService.getById(activityId);
        verifyActivity(activityEntity);

        return activityEntity;
    }


    public MallActivityEntity verifyActivityByCode(String code) {
        if (StringUtils.isBlank(code)){
            throw new BusinessException("活动标识不能为空");
        }
        /*获得活动信息*/
        MallActivityEntity activityEntity = mallActivityService.getByCode(code);
        verifyActivity(activityEntity);

        return activityEntity;
    }

    public MallActivityEntity getActivityByCode(String code) {
        if (StringUtils.isBlank(code)){
            throw new BusinessException("活动标识不能为空");
        }
        /*获得活动信息*/
        MallActivityEntity activityEntity = mallActivityService.getByCode(code);

        return activityEntity;
    }

    private void verifyActivity(MallActivityEntity activityEntity) {
        /*验证活动是否有效*/
        if (activityEntity ==null
                || Constant.IsNot.NO.getValue().equals(activityEntity.getIsShow())
                || Constant.IsNot.NO.getValue().equals(activityEntity.getIsDelete())){
            throw new BusinessException("无效活动信息");
        }

        /*验证活动是否在启动期限内*/
        String activityDate = activityEntity.getActivityDate();
        String activityTime = activityEntity.getActivityTime();
        if (StringUtils.isBlank(activityDate)|| StringUtils.isBlank(activityTime)){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.ACTIVITY_DATE_NOT);
        }
        Date date =new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        boolean bool=false;

        /*//验证日期,有多个时间段，看是否在一个时间段中*/
        JSONArray array = JSONArray.parseArray(activityDate);
        for (int i=0;i<array.size();i++){
            JSONArray jsonArray = array.getJSONArray(i);
            if (DateUtils.compare(date,jsonArray.getString(0)
                    ,jsonArray.getString(1),sf)){
                bool=true;
                break;
            }
        }
        if (!bool){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.ACTIVITY_DATE_NOT);
        }

        /*验证时间*/
        SimpleDateFormat timeSF = new SimpleDateFormat("HH:mm");
        bool=false;
        JSONArray timejson = JSONArray.parseArray(activityTime);
        for (int i=0;i<timejson.size();i++){
            JSONArray jsonArray = timejson.getJSONArray(i);
            if (DateUtils.compareTime(date,jsonArray.getString(0),jsonArray.getString(1),timeSF)){
                bool=true;
                break;
            }
        }
        if (!bool){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.ACTIVITY_DATE_NOT);
        }
    }
}
